package com.cxh.rmsq.viewmodel;

import android.app.Application;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.cxh.rmsq.db.model.UserInfo;
import com.cxh.rmsq.im.IMManager;

import java.util.List;

import com.cxh.rmsq.db.model.MineBean;
import com.cxh.rmsq.model.Resource;
import com.cxh.rmsq.model.Resource2;
import com.cxh.rmsq.model.Result;
import com.cxh.rmsq.model.Result2;
import com.cxh.rmsq.task.UserTask;
import com.cxh.rmsq.utils.SingleSourceLiveData;
import com.cxh.rmsq.utils.log.SLog;

public class UserInfoViewModel extends AndroidViewModel {

    private final UserTask userTask;
    private IMManager imManager;
    private SingleSourceLiveData<Result2<MineBean>> report = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Result2<String>> userStrself = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource2<String>> userStr = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource2<MineBean>> userselfInfo = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<UserInfo>> userInfo = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<Result>> setNameResult = new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<Result>> uploadPotraitResult =
            new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<Result>> changePasswordResult =
            new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<Result>> setStAccountResult =
            new SingleSourceLiveData<>();
    private SingleSourceLiveData<Resource<Result>> setGenderResult = new SingleSourceLiveData<>();

    private SingleSourceLiveData<Resource<Void>> deleteAccountResult = new SingleSourceLiveData<>();

    public UserInfoViewModel(@NonNull Application application) {
        super(application);
        imManager = IMManager.getInstance();
        userTask = new UserTask(application);
        requestUserInfo(imManager.getCurrentId());
        requestUserSelfInfo(imManager.getCurrentId());
    }

    public UserInfoViewModel(String userId, @NonNull Application application) {
        super(application);
        userTask = new UserTask(application);
        requestUserInfo(userId);
        requestUserSelfInfo(userId);
    }

    /**
     * 上传
     *
     * @return
     */
    public LiveData<Resource2<String>> submitAuthentication() {
        return userStr;
    }

    /**
     * 更新个人信息
     *
     * @return
     */
    public LiveData<Result2<String>> changePwd() {
        return userStrself;
    }

    /**
     * 账号申诉
     *
     * @return
     */
    public LiveData<Result2<String>> accountAppeal() {
        return userStrself;
    }

    /**
     * 举报用户
     *
     * @return
     */
    public LiveData<Result2<MineBean>> reportUser() {
        return report;
    }

    /**
     * 举报社区
     *
     * @return
     */
    public LiveData<Result2<MineBean>> reportCommunity() {
        return report;
    }

    /**
     * 更新个人信息
     *
     * @return
     */
    public LiveData<Resource2<String>> changePhone() {
        return userStr;
    }

    /**
     * 更新个人信息
     *
     * @return
     */
    public LiveData<Resource2<String>> updateUserinfo() {
        return userStr;
    }

    /**
     * 上传
     *
     * @return
     */
    public LiveData<Resource2<String>> upload() {
        return userStr;
    }

    /**
     * 获取 UserInfo
     *
     * @return
     */
    public LiveData<Resource2<MineBean>> getUserSelfInfo() {
        return userselfInfo;
    }

    /**
     * 获取 UserInfo
     *
     * @return
     */
    public LiveData<Resource<UserInfo>> getUserInfo() {
        return userInfo;
    }

    /**
     * 设置name 结果
     *
     * @return
     */
    public LiveData<Resource<Result>> getSetNameResult() {
        return setNameResult;
    }

    /**
     * 设置 StAccount 结果
     *
     * @return
     */
    public LiveData<Resource<Result>> getSetStAccountResult() {
        return setStAccountResult;
    }

    /**
     * 设置性别结果
     *
     * @return
     */
    public LiveData<Resource<Result>> getSetGenderResult() {
        return setGenderResult;
    }

    /**
     * 上传头像结果
     *
     * @return
     */
    public LiveData<Resource<Result>> getUploadPortraitResult() {
        return uploadPotraitResult;
    }

    /**
     * 密码修改
     *
     * @return
     */
    public LiveData<Resource<Result>> getChangePasswordResult() {
        return changePasswordResult;
    }

    /**
     * 销户
     */
    public LiveData<Resource<Void>> getDeleteAccountResult() {
        return deleteAccountResult;
    }

    /**
     * 设置用户名
     *
     * @param newName
     */
    public void setName(String newName) {
        setNameResult.setSource(userTask.setMyNickName(newName));
    }

    /**
     * 设置自己的 SealTalk 账号
     *
     * @param stAccount
     */
    public void setStAccount(String stAccount) {
        setStAccountResult.setSource(userTask.setStAccount(stAccount));
    }

    /**
     * 设置性别
     *
     * @param gender
     */
    public void setGender(String gender) {
        setGenderResult.setSource(userTask.setGender(gender));
    }

    /**
     * 上传头像
     *
     * @param uri
     */
    public void uploadPortrait(Uri uri) {
        uploadPotraitResult.setSource(userTask.setPortrait(uri));
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     */
    public void changePassword(String oldPassword, String newPassword) {
        changePasswordResult.setSource(userTask.changePassword(oldPassword, newPassword));
    }

    /**
     * 请求用户信息
     *
     * @param userId
     */
    public void requestUserSelfInfo(String userId) {
        SLog.d("ss_usertask", "userId == " + userId);
        userselfInfo.setSource(userTask.getUserSelfInfo(userId));
    }

    /**
     * 提交实名认证
     *
     * @param
     */
    public void submitAuthentication(String userId, String id, String realName, String idNumber, String zmPhoto, String fmPhoto) {
        SLog.d("ss_usertask", "img == ");
        userStr.setSource(userTask.setauthentication(userId, id, realName, idNumber, zmPhoto, fmPhoto));
    }

    /**
     * 更新个人信息
     *
     * @param
     */
    public void updateUserinfo(String avatar, String nickname, String sex, String birthday, String remark,
                               String constellation, String provinceName, String provinceCode,
                               String cityName, String cityCode, List<String> photoList, String address,
                               String highSchool, String industry, String mbti, String wxPayNum, String zfbPayNum) {
        SLog.d("ss_usertask", "img == ");
        userStr.setSource(userTask.updateUserinfo(avatar, nickname, sex, birthday, remark,
                constellation, provinceName, provinceCode, cityName, cityCode, photoList, address, highSchool, industry, mbti, wxPayNum, zfbPayNum));
    }

    /**
     * 修改密码
     *
     * @param
     */
    public void changePwd(String password) {
        userStrself.setSource(userTask.changePwd(password));
    }

    /**
     * 账号申诉
     *
     * @param
     */
    public void accountAppeal(String deviceId, String userId, String content, String email, int status, String image) {
        userStrself.setSource(userTask.accountAppeal(deviceId, userId, content, email, status, image));
    }

    /**
     * 举报用户
     *
     * @param
     */
    public void reportUser(int reportedUserId, String reportingUserId, int reason, String description, int status, int type, String reportsList) {
        report.setSource(userTask.reportUser(reportedUserId, reportingUserId, reason, description, status, type, reportsList));
    }

    /**
     * 举报社区
     *
     * @param
     */
    public void reportCommunity(String reportedUserId, String userId, String contentId, String reason, String content, int status, int type, String image) {
        report.setSource(userTask.reportCommunity(reportedUserId, userId, contentId, reason, content, status, type, image));
    }

    /**
     * 上传图片
     *
     * @param img
     */
    public void uploadImg(String img) {
        userStr.setSource(userTask.uploadImg(img));
    }

    /**
     * 请求用户信息
     *
     * @param userId
     */
    public void requestUserInfo(String userId) {
        SLog.d("ss_usertask", "userId == " + userId);
        userInfo.setSource(userTask.getUserInfo(userId));
    }

    /**
     * 退出
     */
    public void logout() {
        imManager.logout();
        userTask.logout();
    }

    /**
     * 账号注销
     */
    public void accountDelete() {
        imManager.logout();
        userTask.accountDelete();
    }

    /**
     * 销户
     */
    public void deleteAccount() {
        deleteAccountResult.setSource(userTask.deleteAccount());
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory {
        private String userId;
        private Application application;

        public Factory(String userId, Application application) {
            this.userId = userId;
            this.application = application;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            try {
                return modelClass
                        .getConstructor(String.class, Application.class)
                        .newInstance(userId, application);
            } catch (Exception e) {
                throw new RuntimeException("Cannot create an instance of " + modelClass, e);
            }
        }
    }
}
